package com.example.myapplication.vision.idcard.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.hardware.camera2.CameraCharacteristics;
import android.util.Size;

/**
 * Create By TreeRoot
 * Create Time: 2024/01/23 10:07
 * Class Name: DrawUtils
 * Description:
 *
 * @author TreeRoot
 */
class DrawUtils {

    /**
     * 绘制人脸框
     *
     * @param canvas            画布
     * @param rect              人脸框位置
     * @param color             人脸框颜色
     * @param faceRectThickness 人脸框厚度
     */
    static void drawFaceRect(Canvas canvas, Rect rect, int color, int faceRectThickness) {
        if (canvas == null || rect == null) {
            return;
        }
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(faceRectThickness);
        paint.setColor(color);
        Path mPath = new Path();
        mPath.moveTo(rect.left, rect.top + (float) rect.height() / 4);
        mPath.lineTo(rect.left, rect.top);
        mPath.lineTo(rect.left + (float) rect.width() / 4, rect.top);
        mPath.moveTo(rect.right - (float) rect.width() / 4, rect.top);
        mPath.lineTo(rect.right, rect.top);
        mPath.lineTo(rect.right, rect.top + (float) rect.height() / 4);
        mPath.moveTo(rect.right, rect.bottom - (float) rect.height() / 4);
        mPath.lineTo(rect.right, rect.bottom);
        mPath.lineTo(rect.right - (float) rect.width() / 4, rect.bottom);
        mPath.moveTo(rect.left + (float) rect.width() / 4, rect.bottom);
        mPath.lineTo(rect.left, rect.bottom);
        mPath.lineTo(rect.left, rect.bottom - (float) rect.height() / 4);
        canvas.drawPath(mPath, paint);
    }

    /**
     * 唤起相机拍照片获得一张图片的数据、宽高
     */
    static class CameraData {
        byte[] data;
        int width;
        int height;
    }

    //todo:拍照


    /**
     * 调整人脸框位置
     *
     * @param rect        人脸框位置
     * @param previewSize 预览尺寸
     * @param canvasSize  画布尺寸
     * @param cameraOri   相机方向
     * @param mCameraId   相机ID
     * @return 调整后的人脸框位置
     */
    public static Rect adjustRect(Rect rect, Size previewSize, Size canvasSize, int cameraOri, int mCameraId) {
        if (rect == null) {
            return null;
        }

        int previewWidth = previewSize.getWidth();
        int previewHeight = previewSize.getHeight();
        int canvasWidth = canvasSize.getWidth();
        int canvasHeight = canvasSize.getHeight();

        if (canvasWidth < canvasHeight) {
            int t = previewHeight;
            previewHeight = previewWidth;
            previewWidth = t;
        }

        float widthRatio = (float) canvasWidth / (float) previewWidth;
        float heightRatio = (float) canvasHeight / (float) previewHeight;

        if (cameraOri == 0 || cameraOri == 180) {
            rect.left *= widthRatio;
            rect.right *= widthRatio;
            rect.top *= heightRatio;
            rect.bottom *= heightRatio;
        } else {
            rect.left *= heightRatio;
            rect.right *= heightRatio;
            rect.top *= widthRatio;
            rect.bottom *= widthRatio;
        }

        Rect newRect = new Rect();

        switch (cameraOri) {
            case 0:
                if (mCameraId == CameraCharacteristics.LENS_FACING_FRONT) {
                    newRect.left = canvasWidth - rect.left;
                    newRect.right = canvasWidth - rect.right;
                } else {
                    newRect.left = rect.left;
                    newRect.right = rect.right;
                }
                newRect.top = rect.top;
                newRect.bottom = rect.bottom;
                break;
            case 90:
                newRect.right = canvasWidth - rect.top;
                newRect.left = canvasWidth - rect.bottom;
                if (mCameraId == CameraCharacteristics.LENS_FACING_FRONT) {
                    newRect.top = canvasHeight - rect.left;
                    newRect.bottom = canvasHeight - rect.right;
                } else {
                    newRect.top = rect.left;
                    newRect.bottom = rect.right;
                }
                break;
            case 180:
                newRect.top = canvasHeight - rect.bottom;
                newRect.bottom = canvasHeight - rect.top;
                if (mCameraId == CameraCharacteristics.LENS_FACING_FRONT) {
                    newRect.left = rect.left;
                    newRect.right = rect.right;
                } else {
                    newRect.left = canvasWidth - rect.right;
                    newRect.right = canvasWidth - rect.left;
                }
                break;
            case 270:
                newRect.left = rect.top;
                newRect.right = rect.bottom;
                if (mCameraId == CameraCharacteristics.LENS_FACING_FRONT) {
                    newRect.top = rect.left;
                    newRect.bottom = rect.right;
                } else {
                    newRect.top = canvasHeight - rect.right;
                    newRect.bottom = canvasHeight - rect.left;
                }
                break;
            default:
                break;
        }

        return newRect;
    }


}
